package com.hongyi.modelcontest.modular.contest.controller;

import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hongyi.modelcontest.core.common.annotion.Permission;
import com.hongyi.modelcontest.core.common.exception.BizExceptionEnum;
import com.hongyi.modelcontest.core.common.page.LayuiPageFactory;
import com.hongyi.modelcontest.core.util.IdUtil;
import com.hongyi.modelcontest.modular.contest.entity.ContestInfo;
import com.hongyi.modelcontest.modular.contest.service.ContestInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author skyet
 * @Date 2019-3-4 09:20:48
 */
@Controller
@RequestMapping("/contest")
public class ContestController extends BaseController {
    private static final String PREFIX = "/modular/contest/contest/";

    @Resource
    private ContestInfoService contestInfoService;

    /**
     * 跳转到赛事管理首页
     */
    @RequestMapping("")
    @Permission
    public String index() {
        return PREFIX + "contest.html";
    }

    @RequestMapping(value = "/list")
    @Permission
    @ResponseBody
    public Object list(@RequestParam(name = "name", required = false) String name) {

        IPage list = this.contestInfoService.list(name);
        return LayuiPageFactory.createPageInfo(list);
    }

    /**
     * 跳转到新增页面
     *
     * @author fengshuonan
     */
    @RequestMapping("/contest_add")
    public String addView() {
        return PREFIX + "contest_add.html";
    }

    /**
     * 跳转到编辑页面
     *
     * @author fengshuonan
     */
    @RequestMapping("/contest_edit")
    public String contestEdit(@RequestParam Long contestId) {
        if (ToolUtil.isEmpty(contestId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        return PREFIX + "contest_edit.html";
    }

    /**
     * 更新
     *
     * @author fengshuonan
     * @Date 2018/12/24 22:43
     */
    @RequestMapping("/edit")
    @Permission
    @ResponseBody
    public ResponseData edit(@Valid ContestInfo contestInfo, BindingResult result) {
        if (result.hasErrors()) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        contestInfoService.updateById(contestInfo);
        return SUCCESS_TIP;
    }

    /**
     * 添加
     */
    @RequestMapping("/add")
    @ResponseBody
    @Permission
    public ResponseData add(@Valid ContestInfo contestInfo, BindingResult result) {
        if (result.hasErrors()) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        contestInfo.setContestId(IdUtil.getDateId());
        this.contestInfoService.save(contestInfo);
        return SUCCESS_TIP;
    }

    /**
     * 删除（逻辑删除）
     *
     * @author fengshuonan
     * @Date 2018/12/24 22:44
     */
    @RequestMapping("/delete")
    @ResponseBody
    @Permission
    public ResponseData delete(@RequestParam Long contestId) {
        if (ToolUtil.isEmpty(contestId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        this.contestInfoService.removeById(contestId);
        return SUCCESS_TIP;
    }

    /**
     * 获取详情页面
     */
    @RequestMapping("/detail")
    @ResponseBody
    @Permission
    public Object detail(@RequestParam String contestId) {
        return contestInfoService.getById(contestId);
    }

}
